package wireless.frequent.action;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.DefaultPieDataset;

import wireless.frequent.pojo.Frequent;
import wireless.util.BinaryHexExchange;

import cm.util.hibernate.HibernateSessionFactory;

public class FrequentPieChart {
	private Session session;
    private Transaction trans;
    private String hql;
    private Query query;
    private List l;
    private int length;
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public String execute() throws Exception
	{
		DefaultPieDataset dataset= new DefaultPieDataset();
		session=HibernateSessionFactory.getSession();
		trans=session.beginTransaction();
		if(16==length)
    	{
    		hql="from Frequent as f where f.length>8";
    		query=session.createQuery(hql);
    	}
    	else{
    		hql="from Frequent as f where f.length=:l";
    		query=session.createQuery(hql);
    		query.setInteger("l",length);//设置查询参数
    	}
		l=query.list();
		  for(Iterator sit=l.iterator();sit.hasNext();)//迭代取值
		     {
			  Frequent frequent=(Frequent)sit.next();
			  String value = frequent.getValue();
			  if(length!=4)
			  {
				value=Long.toHexString(Long.parseLong(value, 2)).toUpperCase();
			  }
			  int count = frequent.getCount();
			  if(count>500)
			  {
				  dataset.setValue(value,count);//设置条形图的值
			  }
		     }
		  JFreeChart chart=ChartFactory.createPieChart3D("频繁串饼图分析(大于500)", dataset, true, false, false);
	        chart.getTitle().setFont(new Font("宋体",Font.BOLD,15));
			PiePlot3D plot=(PiePlot3D)chart.getPlot();
			plot.setLabelGenerator(new StandardPieSectionLabelGenerator( 
					"{0}={1}({2})", NumberFormat.getNumberInstance(), 
					new DecimalFormat("0.00%"))); 
			plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( 
			"{0}={1}({2})",NumberFormat.getNumberInstance(), 
			new DecimalFormat("0.00%"))); 
			plot.setLabelFont(new Font("宋体",Font.BOLD,10));
			plot.setBackgroundPaint(Color.WHITE);
			plot.setBackgroundImageAlpha(0.65f);
			plot.setForegroundAlpha(0.65f); 
			plot.setCircular(false, false); 

			LegendTitle legend=chart.getLegend();
			legend.setItemFont(new Font("宋体",Font.BOLD,10));
			
			HttpServletResponse response = ServletActionContext.getResponse();
			try {
				ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart,
						450, 300);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			
	       
			//提交事务
			session.close();
			//session_user.close();
			System.out.println("成功");
		
			return null;
	}
    
}
